#!/bin/bash

# This script is for BOLD correction of T2* contamnation in VASO.
# It is assumed that the data is organized in the following way:
# 1. The script is in the folder with the data
# 2. The data is organized in pairs of BOLD and VASO runs
# 3. The BOLD and VASO runs are named moco_bold_*.nii.gz and moco_cbv_*.nii.gz
# 4. The sequence is executed in a way that VASO is acquired first
# 5. The script will generate a series of files with the prefix BOLD_*.nii.gz 
# and VASO_*.nii.gz

# This script is dependent on AFNI and LayNii

# more informatio about the necessity and assymptoins arround BOLD  
# correction can be found in the following website:
# https://layerfmri.com/baddi/

echo "Starting now with BOLD correction"

# In case you have mutiple runs with identical taks timing,
# you can average them here. 
3dmean -overwrite -prefix BOLD.nii.gz moco_bold_*.nii.gz
3dmean -overwrite -prefix Nulled.nii.gz  moco_cbv_*.nii.gz 


# upsample in time for BOCO
3dUpsample                                                                  \
    -overwrite -datum short                                                 \
    -prefix Nulled_intemp.nii.gz                                            \
    -n 2                                                                    \
    -input Nulled.nii.gz

3dUpsample                                                                  \
    -overwrite  -datum short                                                \
    -prefix BOLD_intemp.nii.gz                                              \
    -n 2                                                                    \
    -input BOLD.nii.gz

# remove Shift BOLD so that it matches the timing with VASO 
NumVol=`3dinfo -nv BOLD_intemp.nii.gz`
NumVolm2=`ccalc -form int "$NumVol - 2"`
3dTcat                                                                       \
    -overwrite                                                               \
    -prefix BOLD_intemp.nii.gz                                               \
    BOLD_intemp.nii.gz'[0]' BOLD_intemp.nii.gz"[0..${NumVolm2}]"

# perform BOCO with VASO image first
LN_BOCO -Nulled Nulled_intemp.nii.gz -BOLD  BOLD_intemp.nii.gz
mv VASO_LN.nii VASO_intemp.nii.gz

# mean VASO for later use of registration from combination
3dTstat -overwrite -mean -prefix VASO.Mean.nii.gz VASO_intemp.nii.gz'[1..$]'
# Removing the noise outside the brain from division zero by zero
LN_MP2RAGE_DNOISE                                                          \
  -INV1 mean_nulled.nii.gz                                                 \
  -INV2 mean_notnulled.nii.gz                                              \
  -UNI VASO.Mean.nii.gz                                                    \
  -beta 10

# adjust TR in header, because 3D-EPI header that follows COBIDAS
#  definition of TR is not meaningfull for fMRI
3drefit -TR 1.844 BOLD_intemp.nii.gz
3drefit -TR 1.844 VASO_intemp.nii.gz

# FOR QA later in case you want to look at it
LN_SKEW -input BOLD_intemp.nii.gz
LN_SKEW -input VASO_intemp.nii.gz

#copying the mean VASO as structural reference for layerification later
cp VASO.Mean.nii.gz structure.nii.gz

echo "BOCO done completed for runer averaged data"






# repeat this for each run in case you have multiple runs

# idx=100
# for filename in ./moco_bold_*.nii.gz
# do

# NumVol=`3dinfo -nv moco_bold_${idx}.nii.gz`
# NumVolm3=`ccalc -form int "$NumVol - 3"`
# 3dcalc                                                         \
#        -overwrite                                              \
#        -prefix BOLD_${idx}.nii.gz                              \
#        -a  moco_bold_${idx}.nii.gz"[0..${NumVolm3}]"           \
#        -expr 'a' 

# 3dcalc                                                         \
#        -overwrite                                              \
#        -prefix Nulled_${idx}.nii.gz                            \
#        -a  moco_cbv_${idx}.nii.gz"[0..${NumVolm3}]"            \
#        -expr 'a' 

# 3dUpsample                                                     \
#       -overwrite                                               \
#       -datum short                                             \
#       -prefix Nulled_intemp_${idx}.nii.gz                      \
#       -n 2                                                     \
#       -input Nulled_${idx}.nii.gz

# 3dUpsample                                                     \
#       -overwrite                                               \
#       -datum short                                             \
#       -prefix BOLD_intemp_${idx}.nii.gz                        \
#       -n 2                                                     \
#       -input BOLD_${idx}.nii.gz


# NumVol=`3dinfo -nv BOLD_intemp_${idx}.nii.gz`
# NumVolm2=`ccalc -form int "$NumVol - 2"`

# 3dTcat                                                        \
#   -overwrite                                                  \
#   -prefix BOLD_intemp_${idx}.nii.gz                           \
#   BOLD_intemp_${idx}.nii.gz'[0]' BOLD_intemp_${idx}.nii.gz"[0..${NumVolm2}]"

# LN_BOCO -Nulled Nulled_intemp_${idx}.nii.gz -BOLD BOLD_intemp_${idx}.nii.gz
# mv VASO_LN.nii VASO_intemp_${idx}.nii.gz

# 3drefit -TR 1.844 BOLD_intemp_${idx}.nii.gz
# 3drefit -TR 1.844 VASO_intemp_${idx}.nii.gz

# idx=$(($idx+1))
# done

# perform GLM of BOLD to VASO

#./03afni_GLM.sh BOLD_intemp.nii.gz
#./03afni_GLM.sh VASO_intemp.nii.gz

